package DoublePointer;

import java.util.ArrayList;
import java.util.List;

public class DoublePointer2_twoSquareSum {
    public static void main(String[] args) {
        int target = 100,i=0,j;
        int index_first = 0;
        //定义两个集合，用于存储全部符合条件的结果
        List list_first = new  ArrayList();
        List list_end = new  ArrayList();
        //剪枝操作，降低时间复杂度，对目标数进行开平方
        int index_end = (int)Math.sqrt(target);
        //双指针进行遍历
        while (index_first<index_end){
            int temp = index_first*index_first + index_end *index_end;
            if ( temp == target ){
                //将符合要求的参数添加到集合中
                list_first.add(index_first);
                list_end.add(index_end);
                i++;
                index_end--;
                index_first++;
                //继续遍历
                continue;
            }else if (temp > target){
                index_end--;
            }else {
                index_first++;
            }
        }
        //输出结果
        for (j=0;j<list_first.size();j++){
            System.out.println("第"+ (j+1) +"组:");
            System.out.println(list_first.get(j) +" * "+ list_first.get(j)
                    +" + "+ list_end.get(j)
                    +" * "+ list_end.get(j)
                    +" = " + target);
        }
    }
}
